package com.itheima.executorservice1;

import java.util.concurrent.*;

public class Test2 {

    public static void main(String[] args) {

        //线程池：ExecutorService接口

        // 创建线程池对象

        //方式一：使用线程池的实现类ThreadPoolExecutor声明7个参数来创建线程池对象

        ExecutorService pool = new ThreadPoolExecutor(3, 5,
                10, TimeUnit.SECONDS, new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

        //2. 使用线程池处理Callable任务
        Future<String> f1 = pool.submit(new MyCallable(100));
        Future<String> f2 = pool.submit(new MyCallable(200));
        Future<String> f3 = pool.submit(new MyCallable(300));
        Future<String> f4 = pool.submit(new MyCallable(400));       //复用线程池中的线程

        try {
            System.out.println(f1.get());       //  一般情况下应分开try-catch，防止影响下面代码的执行
            System.out.println(f2.get());
            System.out.println(f3.get());
            System.out.println(f4.get());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
